<?php

namespace App\Http\Controllers\Personnel;

use App\Criteria\Personnel\Station\StationBaseQueryCriteria;
use App\Http\Controllers\Controller;
use App\Http\Controllers\CurlRecordMethod;
use App\Http\Requests\Personnel\StationBaseRequest;
use App\Models\System\Role;
use App\Repositories\Personnel\StationClassRepository;
use App\Repositories\Personnel\StationRepository;
use App\Repositories\System\PermissionRepository;
use App\Repositories\System\UserRepository;
use App\Validators\Personnel\StationValidator;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Prettus\Validator\Contracts\ValidatorInterface;
use Prettus\Validator\Exceptions\ValidatorException;


class StationsController extends Controller{

    use CurlRecordMethod;

    protected $index_page='personnel.station.stationMain';
    protected $add_page='personnel.station.stationAdd';
    protected $edit_page='personnel.station.stationEdit';
    protected $index_permission_page='personnel.station.stationPermissionMain';
    protected $edit_permission_page='personnel.station.stationPermissionEdit';

    protected $repository;
    protected $userRepository;
    protected $stationClassRepository;
    protected $validator;
    protected $request;

    public function __construct(StationRepository $repository,
                                UserRepository $userRepository,
                                StationClassRepository $stationClassRepository,
                                StationValidator $validator,
                                StationBaseRequest $stationBaseRequest){
        $this->repository = $repository;
        $this->validator  = $validator;
        $this->request    = $stationBaseRequest;
        $this->userRepository =$userRepository;
        $this->stationClassRepository =$stationClassRepository;
    }

    public function getBaseQueryCriteria(){
        return new StationBaseQueryCriteria;
    }

    public function index(){
        $stationClasses=$this->stationClassRepository->skipPresenter()->all();
        return view($this->index_page,compact('stationClasses'));
    }

    protected function post_add_data(){
        $all=$this->wrap_user_info($this->request->all(),true);
        try {
            $this->validator->with($all)->passesOrFail(ValidatorInterface::RULE_CREATE);
            DB::transaction(function () use(&$all) {
                $modal=$this->repository->skipPresenter()->create($all);
                $role=new Role();
                $role->name=$modal->id;
                $role->display_name=$modal->id;
                $role->description=$modal->id;
                $role->save();
                $modal->role_id=$role->id;
                $modal->save();
            });

            return $this->return_json_success();
        } catch (ValidatorException $e) {
            return $this->return_json_error($e);
        }
    }

    protected function get_edit_data(){
        $id=$this->request->get('id');
        $editEntity = $this->repository->find($id);
        $stationClasses=$this->stationClassRepository->skipPresenter()->all();
        $users=$this->userRepository->skipPresenter()->all();
        return view($this->edit_page,compact('editEntity','stationClasses','users'));
    }

    public function delete_data(){
        try {
            $ids=Input::get('ids');
            foreach ($ids as $id){
                $this->validator->with(['id'=>$id])->passesOrFail('DELETE');
            }
            DB::transaction(function () {
                $ids=Input::get('ids');
                $models=$this->repository->skipPresenter()->findWhereIn('id',$ids);
                foreach ($models as $model){
                    $model->users()->detach();
                    $model->role()->delete();
                    $model->delete();
                }
            });
            return $this->return_json_success();
        } catch (ValidatorException $e) {
            return $this->return_json_error($e);
        }
    }

    public function permission(){
        return view($this->index_permission_page);
    }

    public function get_permission_edit(PermissionRepository $permissionRepository){
        $role_id=Input::get('role_id');
        $allPermissions=$permissionRepository->skipPresenter()->all();
        $role=Role::find($role_id);
        $rolePermissions=$role->perms;
        return view($this->edit_permission_page,compact('role_id','allPermissions','rolePermissions'));
    }
    public function post_permission_edit(){

        DB::transaction(function () {
            $permissions=Input::get('permissions');
            $role_id=Input::get('role_id');
            $role=Role::find($role_id);
            $role->savePermissions($permissions);
        });
        return $this->return_json_success();
    }
}
